package cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.service.impl;

import cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.req.MCCCreateReq;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.req.MCCListSearchReq;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.resp.MCCDetailListResp;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.satoken.LaddieAndroidCurrentUser;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.satoken.StpLaddieAndroidUtil;
import cn.bmbanjia.zebra2024.comWechat.SendMessageUtil;
import cn.bmbanjia.zebra2024.frame.exception.CodeException;
import cn.bmbanjia.zebra2024.frame.resp.Resp;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.PhoneUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.entity.MarketingCampaignClew;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.mapper.MarketingCampaignClewMapper;
import cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.service.IMarketingCampaignClewService;
import io.github.linpeilie.Converter;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.bmbanjia.zebra2024.apps.laddieAndroid.marketingCampaignClew.entity.table.MarketingCampaignClewTableDef.MARKETING_CAMPAIGN_CLEW;

/**
 * 小哥限时推单线索 服务层实现。
 *
 * @author fugui
 * @since 2024-07-10
 */
@Service
public class MarketingCampaignClewServiceImpl extends ServiceImpl<MarketingCampaignClewMapper, MarketingCampaignClew> implements IMarketingCampaignClewService {

    @Resource
    private Converter converter;

    @Override
    public Resp<List<MCCDetailListResp>> list(MCCListSearchReq req) {
        List<MCCDetailListResp> list = listAs(QueryWrapper.create()
                .select(MARKETING_CAMPAIGN_CLEW.MCC_ID,
                        MARKETING_CAMPAIGN_CLEW.MCC_STATUS,
                        MARKETING_CAMPAIGN_CLEW.ORDER_ID,
                        MARKETING_CAMPAIGN_CLEW.CUSTOMER_NAME,
                        MARKETING_CAMPAIGN_CLEW.CUSTOMER_PHONE,
                        MARKETING_CAMPAIGN_CLEW.CUSTOMER_REMARK,
                        MARKETING_CAMPAIGN_CLEW.MCC_COMMISSION,
                        MARKETING_CAMPAIGN_CLEW.CREATE_TIME)
                .where(MARKETING_CAMPAIGN_CLEW.LADDIE_ID.eq(StpLaddieAndroidUtil.currentUser().getLaddieId()))
                .orderBy(MARKETING_CAMPAIGN_CLEW.MCC_ID.desc())
                .limit(req.offset(), req.getLength()), MCCDetailListResp.class);

        for (MCCDetailListResp details : list) {
            details.setCustomerPhone(PhoneUtil.hideBetween(details.getCustomerPhone()).toString());
            details.setMccStatusVal(details.getMccStatus().getValue());
            details.setMccStatusStr(details.getMccStatus().getLabel());
        }

        return Resp.success(list);
    }

    @Override
    public Resp<Void> create(MCCCreateReq req) {

        MarketingCampaignClew clew = converter.convert(req, MarketingCampaignClew.class);
        LaddieAndroidCurrentUser user = StpLaddieAndroidUtil.currentUser();
        clew.setLaddieId(user.getLaddieId());
        clew.setLaddieName(user.getLaddieName());

        boolean save = save(clew);
        if (save) {
            SendMessageUtil.send("小哥 " + user.getLaddieName() + " 提交了新搬家线索（限时活动 订单管理 -> 小哥限时推单线索）",
                    SendMessageUtil.URL_KEFU, SendMessageUtil.USERS_KEFU);
            return Resp.success();
        }

        return Resp.error();
    }

}
